package com.tk.exam.filter;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import java.io.File;
import java.io.IOException;

@Component
@Order(-20000)//控制过滤器的级别
public class TomcatTmpDirFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        String contentType = request.getContentType();
        if (contentType != null && contentType.contains("multipart/form-data")) {
            File location = ((File) request.getServletContext().getAttribute(
                    ServletContext.TEMPDIR));
            if (!location.exists()) {
                location.mkdirs();
            }
        }
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }
}
